package com.esb.transport.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.esb.transport.channel.Channel;
import com.esb.transport.channel.NettyChannel;
import com.esb.transport.protocal.Invocation;

public class DefaultHandler extends ChannelInboundMessageHandlerAdapter<Invocation> {
	private static final Log logger = LogFactory.getLog(DefaultHandler.class);
	
	public void messageReceived(ChannelHandlerContext ctx, Invocation msg) throws Exception {
		Channel channel = NettyChannel.wrapperChannel(ctx.channel());
		channel.receive(msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.warn("Unexpected exception from downstream.", cause);
        ctx.close();
    }
    
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
    	Channel channel = NettyChannel.wrapperChannel(ctx.channel());
    	channel.opened();
    }
    
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    	Channel channel = NettyChannel.wrapperChannel(ctx.channel());
    	channel.closed();
    }
}
